//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package graphics;

import database.DBAdapter;
import game.CPU;
import game.Obolo;
import game.Player;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FinishDialog extends Dialog {
	private LinearLayout parentLay;
	TextView txtInfo;
	private LinearLayout loserDeckLay;
	Button btnAccept;
	private OboloGraphicsView oboloWon;
	private Player winner;
	private Player looser;
	private Context context;

	public FinishDialog(Context context) {
		super(context);
		this.context = context;
		loadContent();
	}

	public FinishDialog(Context context, Player winner, Player looser) {
		// Retorna el jugar guanyador amb el deck del adversari
		super(context);

		this.context = context;
		this.winner = winner;
		this.looser = looser;

		// Crea el Layout del dialog
		loadContent();

		setTitle(winner.getName() + " es el guanyador!");

		// Es mira si el guanyador es huma i si el perdedor es la maquina, per
		// tant el jugador, pot triar
		// un obolo de la maquina, aixo es comproba per tal de que si juguen 2
		// jugadors no puguin
		// guanyar obolos facilment
		if (winner.isHuman() && !looser.isHuman()) {
			// Assigna un nou estat inicial als views
			txtInfo.setText("Selecciona un Obolo del teu rival");
			btnAccept.setEnabled(false);
			for (Obolo obolo : winner.getDeck()) {
				// Carga el deck del adversari per poder robar una carta
				OboloGraphicsView oboloView = new OboloGraphicsView(
						getContext(), obolo);
				oboloView.setOnTouchListener(new OnTouchListener() {
					// Seleccio del premi
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (oboloWon != null)
							oboloWon.setBackgroundColor(Color.TRANSPARENT);
						oboloWon = (OboloGraphicsView) v;
						oboloWon.setBackgroundColor(Color.YELLOW);
						btnAccept.setEnabled(true);
						return true;
					}
				});
				loserDeckLay.addView(oboloView);
			}
		} else {
			btnAccept.setEnabled(true);
			if (!winner.isHuman() && looser.isHuman()) {
				CPU cpuWinner = (CPU) winner;
				oboloWon = new OboloGraphicsView(getContext(),
						cpuWinner.stealObolo(looser.getDeck()));
				loserDeckLay.addView(oboloWon);

				DBAdapter dbPlayerObolo = new DBAdapter(context);
				dbPlayerObolo.open();
				Cursor cObolo = dbPlayerObolo.getObolo(oboloWon.getObolo());
				long idPlayer = 1;
				long idObolo = cObolo.getLong(cObolo.getColumnIndex("idObolo"));

				Cursor cPlayerObolo = dbPlayerObolo.getPlayerObolo(idPlayer,
						idObolo);
				cPlayerObolo.moveToFirst();
				int quantity = cPlayerObolo.getInt(cPlayerObolo.getColumnIndex("quantity"));
				// Mirem la quantitat del obolo seleccionat que te el jugador
				if (quantity == 1) {
					dbPlayerObolo.deletePlayerObolo(idPlayer, idObolo);
				} else {
					quantity--;
					dbPlayerObolo
							.updatePlayerObolo(idPlayer, idObolo, quantity);
				}
				dbPlayerObolo.close();
			}
		}
	}

	private void loadContent() {
		// Crea els views del dialog
		parentLay = new LinearLayout(context);
		txtInfo = new TextView(context);
		loserDeckLay = new LinearLayout(context);
		btnAccept = new Button(context);

		// Crea el Layout del dialog
		parentLay.setOrientation(LinearLayout.VERTICAL);
		parentLay.addView(txtInfo);
		parentLay.addView(loserDeckLay);
		parentLay.addView(btnAccept);
		setContentView(parentLay);

		// Assigna el estat inicial als views del dialog
		setCancelable(true);
		setTitle("Empat! Ben jugat");
		loserDeckLay.setGravity(Gravity.CENTER);
		loserDeckLay.setOrientation(LinearLayout.HORIZONTAL);
		btnAccept.setText("Tornar");
		txtInfo.setText("");

		btnAccept.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (oboloWon != null && winner.isHuman()) {
					Obolo obolo = oboloWon.getObolo();

					DBAdapter dbAdapter = new DBAdapter(context);
					dbAdapter.open();

					Cursor cObolo = dbAdapter.getObolo(obolo);
					long idPlayer = 1;
					long idObolo = cObolo.getLong(cObolo.getColumnIndex("idObolo"));

					Cursor cPlayerObolo = dbAdapter.getPlayerObolo(idPlayer, idObolo);
					if(cPlayerObolo.moveToFirst()){
						long quantity = cPlayerObolo.getLong(cPlayerObolo.getColumnIndex("quantity"));
						quantity++;
						dbAdapter.updatePlayerObolo(idPlayer, idObolo, quantity);
					}
					else{
						dbAdapter.insertPlayerObolo(idPlayer, idObolo, 1);
					}
					
					
					dbAdapter.close();
				}

				getOwnerActivity().finish();
				return true;
			}
		});
	}

}
